package com.jasonm.rssminer.tasks;

import java.util.Arrays;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.amazonaws.services.simpleemail.model.Body;
import com.amazonaws.services.simpleemail.model.Content;
import com.amazonaws.services.simpleemail.model.Destination;
import com.amazonaws.services.simpleemail.model.Message;
import com.amazonaws.services.simpleemail.model.SendEmailRequest;
import com.jasonm.rssminer.dto.FeedDTO;
import com.jasonm.rssminer.service.RssService;
import com.jasonm.rssminer.utils.EmailResults;

@Component
public class RssTasks {
	
	@Autowired
	@Qualifier("rssService")
	private RssService rssService; 
	
	@Autowired
	@Qualifier("emailResults")
	private EmailResults emailResults;
	
	private final static Logger log = Logger.getLogger(RssTasks.class);
	
	@Scheduled(cron="0 38 1 * * ?")
	public void runTasks(){
		rssService.loadRssFeeds();
		List<FeedDTO> results = rssService.getSearchResults("gold");
		
		for (FeedDTO feed : results) {
			log.info(feed);
			//emailResults.sendEmail(buildEmail(feed));
		}
		//rssService.cleanSolr();
	}
	
	
	private SendEmailRequest buildEmail(FeedDTO feed){
		
		SendEmailRequest request = new SendEmailRequest();
		
		Message msg = new Message();
		
		msg.setBody(new Body(new Content(feed.getTitle()+"\n"+feed.getLink()+"\n"+feed.getDescription())));
		msg.setSubject(new Content("Your Results"));
		
		request.setMessage(msg);
		request.setDestination(new Destination(Arrays.asList("jasonm@gredo.ca")));
		request.setSource("jason.mcdonald@gmail.com");
		
		return request;
		
		
	}
	
	

}
